package com.rose.doorto.footbathdoor.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.rose.doorto.common.constant.AuthServerConstant;
import com.rose.doorto.footbathdoor.mapper.AddressMapper;
import com.rose.doorto.footbathdoor.pojo.Address;
import com.rose.doorto.footbathdoor.pojo.User;
import com.rose.doorto.footbathdoor.service.AddressService;
import com.rose.doorto.footbathdoor.util.PageUtils;
import com.rose.doorto.footbathdoor.util.Query;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author lqc
 * @since 2024-07-11
 */
@Service
@Transactional
public class AddressServiceImpl extends ServiceImpl<AddressMapper, Address> implements AddressService {

    @Autowired
    private AddressMapper addressMapper;


    /**
     * 新增收货地址列表
     * @param address
     * @return
     */
    @Override
    public int insertAddress(Address address) {

        return addressMapper.insertAddress(address);
    }

    /**
     * 设置默认地址
     * @param status
     * @return
     */
    @Override
    public int updateStatus(int status) {
        return addressMapper.updateStatus(status);
    }

    /**
     * 获取当前用户的收货地址列表
     * @param userId
     * @return
     */
    @Override
    public List<Address> getAddressByUserId(IPage<Address> page, @Param("userId")
                                            Integer userId) {

        return addressMapper.getAddressByUserId(page,userId);
    }

    /**
     * 获取当前用户的收货地址列表
     *
     * @param userId
     * @return
     */
//    @Override
//    public Page<Address> getAddressPage(Integer userId, PageParam pageParam) {
//        //P page, @Param("ew") Wrapper<T> queryWrapper
//        Page<Address> page = new Page<>(pageParam.getPageNo(), pageParam.getPageSize());
//        QueryWrapper<Address> wrapper = new QueryWrapper<>();
//        wrapper.eq("userId",userId);
//        Page<Address> selected = addressMapper.selectPage(page, wrapper);
//        return selected;
//    }

    @Override
    public PageUtils getAddressPage(Map<String, Object> params) {
        IPage<Address> page = this.page(new Query<Address>().getPage(params),
                new QueryWrapper<>());
        return new PageUtils(page);
    }

    /**
     * 根据收货地址id删除收货地址方法2
     * @param id
     * @return
     */
    public int delete(String id) {
        return addressMapper.delete(id);
    }

    /**
     * 批量删除收货地址
     * @param ids
     * @return
     */

    public String deleteBatch(List<String> ids) {
        int i = addressMapper.deleteBatchIds(ids);
        if (i>0){
            return "删除成功";
        }else {
            return "删除失败";
        }

    }

    /**
     * 设置用户的默认收货地址
     * @param userId   用户身份ID
     * @param id       收货地址列表ID
     * @return
     */
    @Override
    public int setDefauleAddress(int userId, Integer id) {
        return addressMapper.setDefauleAddress(userId,id);
    }



    /**
     * 取消默认地址的设置
     * @param userId
     * @param id
     * @return
     */
    @Override
    public int cancelDefauleAddress(int userId, Integer id) {
        return addressMapper.cancelDefauleAddress(userId,id);
    }



    @Override
    public Address getDefauleAddressByUserId(Integer userId) {
        return addressMapper.getDefauleAddressByUserId(userId);
    }

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        QueryWrapper<Address> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id",1);
        IPage<Address> page = this.page(
                new Query<Address>().getPage(params),
                queryWrapper
        );
        return new PageUtils(page);
    }

    /**
     * 分页查询我的收货地址列表
     * @param params
     * @param session
     * @return
     */
    @Override
    public PageUtils getCurrentAddressList(Map<String, Object> params, HttpSession session) {
        User user = (User) session.getAttribute(AuthServerConstant.LOGIN_USER_KEY);
        //获取当前登录用户的所有收货地址
        IPage<Address> page = this.page(new Query<Address>().getPage(params),
                new QueryWrapper<Address>().eq("userId",user.getId()).orderByDesc("id"));
        return new PageUtils(page);
    }


    public IPage<Address> selectPage(int pageNum, int pageSize) {
        Page<Address> page = new Page<>(pageNum, pageSize);
        return this.page(page);
    }


}
